//
//  MqInfoPictureShowViewController.m
//  Meiqi
//
//  Created by 王郑 on 16/1/15.
//  Copyright © 2016年 KFC. All rights reserved.
//

#import "MqInfoPictureShowViewController.h"
#import "MqInfoPictureDetailCollectionViewCell.h"
#import "MqInfoPictureDetailModel.h"
#import "MqInfoPictureShowMoreViewController.h"

@interface MqInfoPictureShowViewController ()<UICollectionViewDataSource,UICollectionViewDelegate>

@property (strong, nonatomic) UICollectionView *collectionView;
// 数据源数组
@property (strong, nonatomic) NSMutableArray *listArray;

@property (strong, nonatomic) UILabel *titleLabel;


// 保存图片地址的数组
@property (strong, nonatomic) NSMutableArray *imageURLArray;


@end

@implementation MqInfoPictureShowViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
#pragma maek ----- 数据源数组
    _listArray = [NSMutableArray array];
    
    _imageURLArray = [NSMutableArray array];
    
#pragma maek ----- 自定义导航栏
    [self addNavBar];
    
#pragma maek ----- 添加集合视图
    [self addCollectView];
    
#pragma mark ---- 解析数据
    [self setUpCollectData];

    
    
}

- (void)addCollectView
{
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc]init];
    // 设置属性
    layout.itemSize = CGSizeMake(170*kWidthBase, 170*kHeightBase);
    layout.sectionInset = UIEdgeInsetsMake(10*kWidthBase, 10*kWidthBase, 10*kWidthBase, 10*kWidthBase);
    // 设置最小行边距
    layout.minimumLineSpacing = 10*kHeightBase;
    // 设置最小列边距
    layout.minimumInteritemSpacing = 10*kWidthBase;
    // 设置滑动的方向
    layout.scrollDirection = UICollectionViewScrollDirectionVertical;
    _collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 64*kHeightBase, kScreenWidth, kScreenHeight - (64 + 49) * kHeightBase) collectionViewLayout:layout];
    
    // 设置颜色
    _collectionView.backgroundColor = [UIColor whiteColor];
    
    // 设置代理和数据源
    _collectionView.delegate = self;
    _collectionView.dataSource = self;
    
    // 注册cell
    [_collectionView registerClass:[MqInfoPictureDetailCollectionViewCell class] forCellWithReuseIdentifier:@"pictureDetailCell"];
    
    [self.view addSubview:_collectionView];
}


- (void)addNavBar
{
    
    UIView *barView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, kScreenWidth, 64 * kHeightBase)];
    barView.backgroundColor = [UIColor yellowColor];
    [self.view addSubview:barView];
    
    
    UIButton *backButton = [UIButton buttonWithType:(UIButtonTypeCustom)];
    backButton.frame = CGRectMake(10 * kWidthBase, 30 * kHeightBase, 20 * kWidthBase, 20 * kHeightBase);
    [backButton setBackgroundImage:[UIImage imageNamed:@"info-back"] forState:(UIControlStateNormal)];
    [backButton addTarget:self action:@selector(backAction:) forControlEvents:(UIControlEventTouchUpInside)];
    [barView addSubview:backButton];
    
    
    _titleLabel = [[UILabel alloc] initWithFrame:CGRectMake((kScreenWidth - 300*kWidthBase)/2, 20 * kHeightBase, 300*kWidthBase, 40*kHeightBase)];
    _titleLabel.text = _titleString;
    _titleLabel.textAlignment = NSTextAlignmentCenter;
    _titleLabel.textColor = [UIColor blackColor];
    [barView addSubview:_titleLabel];
    
    
    
}

- (void)backAction:(UIButton *)button
{
    [self.navigationController popViewControllerAnimated:YES];
    
}


// 解析数据
- (void)setUpCollectData
{
    
    [PKRequestManager requestWithUrlString:_picUrl parDic:nil Method:GET finish:^(NSData *data) {
        
        NSMutableDictionary *dic = [NSJSONSerialization JSONObjectWithData:data options:(NSJSONReadingMutableContainers) error:nil];
        
        NSDictionary *details = dic[@"details"];
        NSArray *morepic = details[@"morepic"];
        for (NSDictionary *oneDic in morepic) {
            
            MqInfoPictureDetailModel *model = [[MqInfoPictureDetailModel alloc] init];
            [model setValuesForKeysWithDictionary:oneDic];
            [_listArray addObject:model];
            
            [_imageURLArray addObject:model.titlePic];
        }
        
        
       [_collectionView reloadData];
        
        
    } error:^(NSError *error) {
        
//        NSLog(@"%@",error);
        
    } path:@"pictureDetailData"];
    
    
}



- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    
    return _listArray.count;
    
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    
    MqInfoPictureDetailCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"pictureDetailCell" forIndexPath:indexPath];
    // 展示数据
    MqInfoPictureDetailModel *model = _listArray[indexPath.row];
    [cell.picImageV sd_setImageWithURL:[NSURL URLWithString:model.titlePic] placeholderImage:[UIImage imageNamed:@"infoplaceholder"]];

 
    return cell;
}







- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    
    
    
    MqInfoPictureShowMoreViewController *psmv = [[MqInfoPictureShowMoreViewController alloc] init];
    psmv.imagesArray = _listArray;
    psmv.indexStart = indexPath.row;
    psmv.type = 4;
    [self presentViewController:psmv animated:YES completion:nil];
     
 
}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
